<?php
namespace Api\Controller;

use Common\Model\Ai\AiTaskModel;
use Common\Model\Fenxiao\FenxiaoUserRecordModel;
use Common\Model\User\UserVipLogModel;
use Common\Util\BaseData;
use Qiniu\Auth;
use Services\Ai\AiService;
use Services\Fenxiao\FenxiaoService;
use Services\Request\TemplateRequest;
use Services\Security\SecurityService;
use Services\Upload\Exceptions\UploadException;
use Services\Upload\OssFileService;
use Services\Upload\UploadService;
use Services\User\UserService;
use Services\Vip\VipService;
use Services\WxGongZhong\WxGongZhongService;
use Think\Controller;
use WechatPayV3\WechatPayV3;

class TestController extends Controller{

	public function test(){

//		$this->getUploadSign();
		// $this->jsapiPayTest();
//		$this->requestTest();
		$this->ossFileTest();
//		$this->fenxiaoTest();
//		$this->securityTest();
//		$this->wxTemplateMessageTest();
//		$this->vipTest();
//		$this->aiTest();

	}
	public function aiTest(){
		$service = new AiService();
//		$service->chat([
//			'bot_id' => '7494920105814802484',
//			'user_id' => 1,
//			'content' => '写一篇关于iphone16手机的介绍'
//		]);

//		$service->retrieve();
		$task = (new AiTaskModel())->where('id',5)->find();
		$service->singleTaskResult($task);
	}

	private function vipTest(){
		$service = new VipService();
//		$result = $service->generateVipCode(100,10);
		$result = (new UserService())->openVip(1,(int)'10',UserVipLogModel::CHANNEL_SYSTEM);
		bin_dump($result);
	}
	private function securityTest(){
		$id = 123;
		$en = SecurityService::encryptData($id);
		$en = SecurityService::decryptData($en);
		bin_dump($en);
	}

	private function wxTemplateMessageTest(){
		$service = new WxGongZhongService();
		$result = $service->sendViewMessage(1,1);
		bin_dump($result);
	}

	public function ossFileTest(){
		$service = new OssFileService();
//		$result = OssFileService::getOssUrl('/a.jpg');
		$result = (new UploadService())->setParams([
			'content' => file_get_contents('https://thirdwx.qlogo.cn/mmopen/vi_32/Q3auHgzwzM7Neohjs6Ue4Bw6EOJ0bud1ENZGCjoxBUWlevm2GS05nBc1mricMvHdqiaX4ibxBNFaV3iciaxGvSiaLjrA/132'),
			'filename' => sprintf('bin_img/%s',generate_random_string(12).time().'.png'),
		])->uploadFile();

		bin_dump($result);
	}

	public function fenxiaoTest(){
		$service = new FenxiaoService();
		$result = $service->setParams([
			'user_id' => 1,
			'parent_user_id' => 8
		])->bind();
		bin_dump($result);
	}
	public function envTest(){
		echo getenv('WX_MECHANT_ID');
	}

	private function jsapiPayTest(){

		try {
			$wxpayV3 = new WechatPayV3();
			$config = $wxpayV3->getWxPayConfig();
			var_dump($config);
			echo '1111';
		} catch (\Exception $e) {
			var_dump($e->getMessage());
		}
	}

	public function messageTest(){
		BaseData::sendMessage(331,1);
	}

	public function getUploadSign(){
		$uploadService = new UploadService();
		bin_dump($uploadService);
		try{

			$expire = time() + 600;
		$qiniuConfig = C('UPLOAD_QINIU_CONFIG');
		//1.构造上传策略：
		$dir = date("Ymd");
		$key = $dir."/".I("get.filename");
		$putPolicy = [
			"scope" => $qiniuConfig["bucket"].":".$key,
			"deadline" => $expire,
			"returnBody" => <<<a
{
      "name": $(fname),
      "etag": $(etag),
      "size": $(fsize),
      "key": $(key),
      "w": $(imageInfo.width),
      "h": $(imageInfo.height),
      "hash": $(etag),
      "link":$(x:link)
}
a
		];

		$auth = new Auth($qiniuConfig["accessKey"],$qiniuConfig["secretKey"]);
		// var_dump($qiniuConfig);die();
		return json_encode([
			"token" => $auth->uploadToken($qiniuConfig["bucket"],$key,$expire,$putPolicy),
			"host" => "https://upload-z1.qiniup.com",
			"dir" => $dir,
			"key" => $key,
			"link" => "https://".$qiniuConfig["domain"]."/".$key
		]);

		}catch(\Exception $e){
			var_dump($e->getMessage());
		}
	}

	public function requestTest(){
		$request = new TemplateRequest();
		$result = $request->demo();
		bin_dump($result);

	}
}
?>
